<?php
/* 
Template Name: Sale page
*/

?>

<?php get_header(); ?>

<div id="left-column">
	<h1 class="page-title">Hàng Sale</h1>
	<ul class="products">
	<?php 

	global $tax;
    global $page;

	$tax = new WP_query();

	// get page
	$page = ((get_query_var("page") != '')? get_query_var("page") : 1);

	// var_dump($page);

	$agrs = array(

			'paged' => $page,

			'showposts' => 12,
		    /* chỉ lấy post dạng product */
		    'post_type' => 'product',

		    'sort_by' => 'id',

		    // query lấy ra những sản phẩm đang sale
		    'meta_query' => array(
	            array(
	                'key' => '_visibility',
	                'value' => array('catalog', 'visible'),
	                'compare' => 'IN'
	            ),
	            array(
	                'key' => '_sale_price',
	                'value' =>  0,
	                'compare'   => '>',
	                'type'      => 'NUMERIC'
	            )
	        )
		);

	$tax->query($agrs);

	// var_dump($tax);

	if($tax->have_posts()) : while($tax->have_posts()) : $tax->the_post();
	?>

	<li>
		<?php 
		global $product;	?>

			<?php woocommerce_get_template_part( 'content', 'sale-product' ); ?>

				
		<?php endwhile; // end of the loop. ?>

		</ul>		

	<?php else : ?>

		<?php if ( ! woocommerce_product_subcategories( array( 'before' => '<ul class="products">', 'after' => '</ul>' ) ) ) : ?>

			<p><?php _e( 'No products found which match your selection.', 'woocommerce' ); ?></p>

		<?php endif; ?>

	<?php endif; ?>

<div class="clear"></div>

<?php get_paging();?>	
	
</div><!-- end #left-column -->

<?php get_sidebar(); ?>

<?php get_footer(); ?>